package com.yun.algorithm.backtracking.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class leetcode216 {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(k, n, 1, new LinkedList<>(), result);
        return result;
    }

    static int count=0;
    private void dfs(int k, int n, int start,
                     LinkedList<Integer> stack,
                     List<List<Integer>> result) {
        count++;
        if (n == 0 && k == 0) {
            result.add(new ArrayList<>(stack));
            return;
        }

        for (int i = start; i < 10; i++) {
            if (n < i) {
                continue;
            }
            if(k==0){
                continue;
            }
            stack.push(i);
            dfs(k - 1, n - i, i + 1, stack, result);
            stack.pop();
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> lists = new leetcode216().combinationSum3(3, 7);
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
        System.out.println(count);
    }
}
